/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;

import BBDD.BaseConection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author David
 */
public class Factura{

    public int getNum_factura() {
        return num_factura;
    }

    public void setNum_factura(int num_factura) {
        this.num_factura = num_factura;
    }

    public String getProductos() {
        return productos;
    }

    public void setProductos(String productos) {
        this.productos = productos;
    }

    public int getValor_total() {
        return valor_total;
    }

    public void setValor_total(int valor_total) {
        this.valor_total = valor_total;
    }

    public String getCliente() {
        return cliente;
    }

    public void setCliente(String cliente) {
        this.cliente = cliente;
    }
    public Integer rescatarNumFactura (){
       Integer numeroFactura = 0;
       
       Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DriverManager.getConnection(
                    BaseConection.getCadenaLocal(),
                    BaseConection.getPassword(),
                    BaseConection.getUser());
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT COUNT(NUM_FACTURA) AS NUM FROM FACTURA");
            if (rs.next()) {
               numeroFactura = Integer.parseInt(rs.getString("NUM"));
            }
            conn.close();
        } catch (SQLException exceptionSql) {
            System.out.print("Error:" + exceptionSql.getMessage());
        }
    return numeroFactura;    
    }
    public boolean insertFactura(Factura fac){
        Factura factura = new Factura();
        Connection conn = null;
        Statement stmt = null;
        boolean flag = false;
 try {
            conn = DriverManager.getConnection(
                    BaseConection.getCadenaLocal(),
                    BaseConection.getUser(),
                    BaseConection.getPassword());
             stmt = conn.createStatement();
             int fila = stmt.executeUpdate("INSERT INTO FACTURA (NUM_FACTURA, PRODUCTOS, VALOR_TOTAL, CLIENTE,NUM_ClIENTE)"
                     + "values ("+Integer.toString(fac.getNum_factura())+",'"+fac.getProductos()+"',"+Integer.toString(fac.getValor_total())+",'"
                    +fac.getCliente()+ "'," +Integer.toString(fac.getNumCliente())+")");
             
             if (fila==0){
                 flag = false;
             } else {
                 flag = true;
             }
             } catch (SQLException exceptionSql) {
            System.out.print("Error:" + exceptionSql.getMessage());
        }
        
        return flag;
    }
    //atributos del objeto
    private int num_factura;
    private String productos;
    private int valor_total;
    private String cliente;

    public int getNumCliente() {
        return numCliente;
    }

    public void setNumCliente(int numCliente) {
        this.numCliente = numCliente;
    }
    private int numCliente;
  
}
